package com.xiechuang.helper;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.MessageDigest;

/**
 * @Description: MD5加密助手
 * @Author: xiechuang
 * @Date: 2023/1/11
 */
public class Md5Helper {

    private static final Logger logger = LoggerFactory.getLogger(Md5Helper.class);
    private static final String CLASS_NAME = Md5Helper.class.getName();

    /**
     * 默认的密码字符串组合，apache校验下载的文件的正确性用的就是默认的这个组合
     */
    protected static char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

    private static String SHA = "SDJFLSDFSDF";

    /**
     * 适用于上G大的文件
     *
     * @param file
     * @return
     * @throws IOException
     */
    public static String encrypt(File file) throws IOException {
        FileInputStream in = null;
        try {
            String value = "";
            in = new FileInputStream(file);
            ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
            byte[] cache = new byte[1048576];
            for (int i = in.read(cache); i != -1; i = in.read(cache)) {
                out.write(cache, 0, i);
            }
            in.close();
            out.close();
            MessageDigest messagedigest = MessageDigest.getInstance("MD5");
            messagedigest.update(out.toByteArray());
            value = bufferToHex(messagedigest.digest());
            return value;
        } catch (Exception ex) {
            logger.error(CLASS_NAME + "：", ex);
            return "";
        } finally {
            if (in != null) {
                in.close();
            }
        }
    }

    public static String encrypt(String s) {
        return encrypt(s.getBytes());
    }

    public static String encrypt(byte[] bytes) {
        try {
            MessageDigest messagedigest = MessageDigest.getInstance("MD5");
            messagedigest.update(bytes);
            return bufferToHex(messagedigest.digest());
        } catch (Exception ex) {
            logger.error(CLASS_NAME + "：", ex);
        }
        return "";
    }

    private static String bufferToHex(byte bytes[]) {
        return bufferToHex(bytes, 0, bytes.length);
    }

    private static String bufferToHex(byte bytes[], int m, int n) {
        StringBuffer stringbuffer = new StringBuffer(2 * n);
        int k = m + n;
        for (int l = m; l < k; l++) {
            appendHexPair(bytes[l], stringbuffer);
        }
        return stringbuffer.toString();
    }


    private static void appendHexPair(byte bt, StringBuffer stringbuffer) {
        char c0 = hexDigits[(bt & 0xf0) >> 4];
        char c1 = hexDigits[bt & 0xf];
        stringbuffer.append(c0);
        stringbuffer.append(c1);
    }

    // 生成 计划签名
    public static String signature(String string) {
        return encrypt(string + SHA);
    }

    public static boolean checkPassword(String password, String md5PwdStr) {
        return encrypt(password).equals(md5PwdStr);
    }

}
